package interfaceunderstand;

interface IFLying {
    void flying();
}

interface ISwimming {
    void swimming();
}

interface IRunning {
    void running();
}
//为什么11，12行不能像16，17行这么写呢？
//因为拿Dog类来说，它只能继承一个类，继承了Animal就不能继承Run( java只有单继承)。所以写到接口里面就解决了不能多继承的问题。
/*class Run {
    void running(){

    }
}*/
//把上面飞和游泳写到Animal类里面不合适，因为不是所有的动物都会飞和游泳。
class Animal {
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println("吃饭！");
    }
}

class Dog extends Animal implements IRunning,ISwimming {

    public Dog(String name,int age) {
        super(name,age);
    }

    @Override
    public void swimming() {
        System.out.println(name + " 正在游泳！");
    }

    @Override
    public void running() {
        System.out.println(name + " 正在跑！");
    }

    @Override
    public void eat() {
        System.out.println(name+"正在吃狗粮！");
    }
}

class Bird extends Animal implements IFLying {

    public Bird(String name, int age) {
        super(name, age);
    }

    @Override
    public void flying() {
        System.out.println(name + "正在飞！");
    }
    @Override
    public void eat() {
        System.out.println(name+"正在鸟粮！");
    }
}

class Duck extends Animal implements IFLying,IRunning,ISwimming {

    public Duck(String name, int age) {
        super(name, age);
    }

    @Override
    public void flying() {
        System.out.println(name + "正在飞！");
    }

    @Override
    public void swimming() {
        System.out.println(name + " 正在游泳！");
    }

    @Override
    public void running() {
        System.out.println(name + " 正在跑！");
    }

    @Override
    public void eat() {
        System.out.println(name+"正在吃鸭粮！");
    }
}

class Roobot implements IRunning {

    @Override
    public void running() {
        System.out.println("机器人正在跑！");
    }
}
public class Test5 {
    //多态
    public static void walk(IRunning iRunning) {
        iRunning.running();
    }
    //多态
    //animal所引用的对象不同就可以实现多态。
    public static void func(Animal animal) {
        animal.eat();
    }

    public static void main(String[] args) {
        walk(new Dog("hello",10));
        walk(new Duck("hello1",20));
        //接口是只要具备了这样的功能都可以。
        // public static void walk(IRunning iRunning) {
        //        iRunning.running();
        //    }
        //而不像 public static void func(Animal animal) {
        //        animal.eat();
        //    }前提还一定要是一个动物(存在继承关系)。
        walk(new Roobot());

        System.out.println("================");
        func(new Dog("hello",10));
        func(new Duck("hello1",20));
    }
}